package com.linkstec.jexl;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.regex.Pattern;

import org.apache.commons.jexl2.Expression;
import org.apache.commons.jexl2.JexlContext;
import org.apache.commons.jexl2.JexlEngine;
import org.apache.commons.jexl2.MapContext;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.Test;

public class JexlTest {
	private static final Logger logger = LogManager.getLogger();

	public static void main(String[] args) {
//		String ex = "false & false | false";
//		String ex = "(a > 1 & true ) |b <= -0.4";
		String ex = "NET_TRANS_IN <= -300000||(TOTAL_ASSET_TOTAL_BST >= 100000&&NET_TRANS_IN_RATE_ASSET <= -0.5)";
		JexlContext jc = new MapContext();
		jc.set("a", 1);
		jc.set("b", -0.5);
		jc.set("NET_TRANS_IN", 30000);
		jc.set("TOTAL_ASSET_TOTAL_BST", 10000);
		jc.set("NET_TRANS_IN_RATE_ASSET", -0.5);
		jc.set("PRD_FIRST_PROFIT_RATE", -0.5);
		long start = System.currentTimeMillis();
		boolean bbb = false;
		for (int i = 0; i < 1000; i++){
			bbb = parseBoolean(ex, jc);
		}
		long end = System.currentTimeMillis();
		logger.info((end - start));
		if (bbb) {
			logger.info("结果是:true");
		} else {
			logger.info("结果是:false");
		}
		
		File file = new File("src/main/resources/Expression.txt");
		String exxx = null;
		try {
			FileReader fileReader = new FileReader(file);
			@SuppressWarnings("resource")
			BufferedReader BufferedReader = new BufferedReader(fileReader);
			while((exxx = BufferedReader.readLine()) != null){
				try {
					logger.info("验证表达式:" + exxx);
					parseBoolean(exxx, jc);
				}catch (Exception e) {
					logger.error("表达式:" + exxx + "有问题！");
					e.printStackTrace();
				}
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
	public static boolean parseBoolean(String ex) {
		Expression e = new JexlEngine().createExpression(ex);
		logger.info(e.evaluate(null));
		long i = (long) e.evaluate(null);
		if (i ==1) {
			return true;
		} else {
			return false;
		}
	}
	public static boolean parseBoolean(String ex, JexlContext jc) {
		Expression e = new JexlEngine().createExpression(ex);
//		logger.info(e.evaluate(jc));
		Object result = e.evaluate(jc);
		if (result instanceof Boolean) {
			return (Boolean)result;
		}
		long i = (long) result;
		if (i ==1) {
			return true;
		} else {
			return false;
		}
	}
	public static void loop() {
	    JexlContext jc = new MapContext();
	    jc.set("a", 1);
	    jc.set("b", "0");
	    jc.set("ans", new StringBuffer());
	    Expression e = new JexlEngine().createExpression("while (a < 10) {a = a + 1;ans.append(b);}");
	    e.evaluate(jc);
	    System.out.println(jc.get("ans"));
	}
	public static void getSet() {
	    TmpTest tmpTest = new TmpTest();
	    tmpTest.setA(1);
	    JexlContext jc = new MapContext();
	    jc.set("tmpTest", tmpTest);
	    jc.set("ans", "");
	    Expression e = new JexlEngine().createExpression("ans = tmpTest.getA()");
	    e.evaluate(jc);
	    System.out.println(jc.get("ans"));
	    e = new JexlEngine().createExpression("ans = tmpTest.setA(2)");
	    e.evaluate(jc);
	    TmpTest tmpTest1 = (TmpTest) jc.get("tmpTest");
	    System.out.println(tmpTest1.getA());
	}
	public static boolean regMatch(String regEx, String str) {
        Pattern pattern = Pattern.compile(regEx);
        return pattern.matcher(str).matches();
    }
    @Test
    public void test1(){
    	
    	JexlContext jc = new MapContext();
    	jc.set("aa", "test jexl");
    	Expression e = new JexlEngine().createExpression("10 > 5 && aa.contains(\"test\")");
    	Object result = e.evaluate(jc);
    	System.out.println(result);
    }
}